package com.hpc.Service.Impl;

import Utils.StringHandlerUtils;
import com.hpc.Service.adminService;
import entity.article;
import entity.message;
import entity.systemConstant;
import entity.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class adminServiceImpl implements adminService {
    public static final Integer ADFLAG = 3;
    @Resource
    private com.hpc.mapper.adminMapper adminMapper;
    @Resource
    private com.hpc.Service.userService userService;
    private StringHttpMessageConverter stringHttpMessageConverter;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int userForbidden(String userId) {
        return adminMapper.userForbidden(userId);
    }

    @Override
    public int unForbidden(String userId) {
        return adminMapper.unForbidden(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUser(String userId) {
        int i = adminMapper.deleteUser(userId);
        if(i != 0){
            //同步删除用户发布文章、评论、喜欢关注等
            adminMapper.deleteUserArticle(userId);
            adminMapper.deleteUserComment(userId);
            //删除redis中用户个人喜欢及关注
            String personallike=userId+"_personallike";
            String keyName=userId+"_follow";
            //删除redis个人喜欢、个人关注
            if(redisTemplate.hasKey(personallike)){
                redisTemplate.delete(personallike);
            }
            if(redisTemplate.hasKey(keyName)){
                redisTemplate.delete(keyName);
            }
            if(redisTemplate.hasKey(systemConstant.USERS_HASFOLLOWS)){
                redisTemplate.opsForList().remove(systemConstant.USERS_HASFOLLOWS,1,keyName);
            }
        }
        return i;
    }

    @Override
    public List<user> allUserInfo() {
        return adminMapper.allUserInfo();
    }

    @Override
    public List<article> allArticleInfo(boolean isNeedAds) {
        //过滤广告
        List<article> articles = adminMapper.allArticleInfo();
        //判断过滤广告
        if(!isNeedAds){
            articles=articles.stream().filter(article -> article.getType() != ADFLAG).collect(Collectors.toList());
        }
//        //获取所有广告
//        List<article> ads = adminMapper.getByType(ADFLAG);
//        //按热度降序排序
//        ads.sort(Comparator.comparing(article::getHot).reversed());
        //对获取的文章内容去除视频video、图片img标签
        articles.forEach(article -> {
            String s = StringHandlerUtils.stripHtmlTags(article.getContent());
            article.setContent(s);
        });
//        ads.forEach(article -> {
//            String s = StringHandlerUtils.stripHtmlTags(article.getContent());
//            article.setContent(s);
//        });
        //将articles按5个拆分成多个小集合
//        List<List<article>> subLists = new ArrayList<>();
//        for (int i = 0; i < articles.size(); i += 5) {
//            List<article> tempList=new ArrayList<>();
//            tempList.addAll(articles.subList(i, Math.min(i + 5, articles.size())));
//            subLists.add(tempList);
//        }
////        往subLists中每个随机插入一条广告，从ads顺序取出，随机数取小集合中的0到集合长度
//        Iterator<List<article>> iterator = subLists.iterator();
//        int index=0;
//        List<article> result = new ArrayList<>();
//        while (iterator.hasNext()) {
//            List<article> subList = iterator.next();
//            List<article> articles1 = Collections.synchronizedList(subList);
//            if (!ads.isEmpty()){
//                int randomIndex = new Random().nextInt(subList.size());
//                if(index < ads.size()){
//                    articles1.add(randomIndex, ads.get(index));
//                }
//            }
//            result.addAll(articles1);
//            index++;
//        }
        return articles;
    }

    @Override
    public int deleteArticle(String articleId) {
        return adminMapper.deleteArticle(articleId);
    }

    @Override
    public int login(String username, String password) {
        int login = adminMapper.login(username, password);
        return login;
    }

    @Override
    public Map<String,Object> getAllMessages() {
        List<message> messages = adminMapper.allMessages();
        ArrayList<user> users = new ArrayList<>();
        for (message message : messages) {
            user userInfoBySerial = userService.getUserInfoBySerial(message.getUserId());
            users.add(userInfoBySerial);
        }
        HashMap<String, Object> infos = new HashMap<>();
        infos.put("messages",messages);
        infos.put("usersInfo",users);
        return infos;
    }

    @Override
    public int deleteMessage(String messageId) {
        return adminMapper.deleteMessage(messageId);
    }

}
